﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace hlighttohtml
{
    class Program
    {

        static void Main(string[] args)
        {
            //if (Directory.Exists("Files"))
            //{
            //    Console.WriteLine("This directory exists");
            //}
            //else
            //{
            //    Directory.CreateDirectory("Files");
            //    Console.WriteLine("Directory creates");
            //}

            string inputfile = "";
            string inputdirectory = "";
            string outputfile = "";
            string css = "";
            string outputdirectory = "";
            string find = "";

            string HelpMessage = "HELP";

            if (args.Length < 6)
            {
                Console.WriteLine(HelpMessage);
                return;
            }

            switch (args[0].ToString())
            {
                case "tohtml":
                    break;
                case "-help":
                    Console.WriteLine(HelpMessage);
                    return;
                default:
                    Console.WriteLine(HelpMessage);
                    return;
            }

            switch (args[1].ToString())
            {
                case "-i":
                    if (!(File.Exists(args[2].ToString())))
                    {
                        Console.WriteLine(HelpMessage);
                        return;
                    }
                    else
                    {
                        inputfile = args[2].ToString();
                    }
                    break;
                case "-idir":
                    if (args.Length < 7)
                    {
                        Console.WriteLine(HelpMessage);
                        return;
                    }
                    switch (args[2].ToString())
                    {
                        case "default":
                            if (Directory.Exists(Environment.CurrentDirectory + "\\Files"))
                            {
                                inputdirectory = Environment.CurrentDirectory + "\\Files";
                            }
                            else
                            {
                                Console.WriteLine(HelpMessage);
                                return;
                            }
                            break;
                        default:
                            if (!(Directory.Exists(args[2].ToString())))
                            {
                                Console.WriteLine(HelpMessage);
                                return;
                            }
                            else
                            {
                                inputdirectory = args[2].ToString();
                            }
                            break;
                    }
                    break;
                default:
                    Console.WriteLine(HelpMessage);
                    return;
            }

            if (args[1].ToString() == "-idir")
            {
                switch (args[3].ToString())
                {
                    case "-a":
                        find = "all";
                        break;
                    case "-c":
                        find = "current";
                        break;
                }

                switch (args[4].ToString())
                {
                    case "default":
                        css = "default";
                        break;
                    default:
                        css = args[4].ToString();
                        break;
                }

                switch (args[5].ToString())
                {
                    case "-odir":
                        break;
                    default:
                        Console.WriteLine(HelpMessage);
                        return;
                }

                switch (args[6].ToString())
                {
                    case "default":
                        outputdirectory = Environment.CurrentDirectory + "\\Output";
                        Directory.CreateDirectory(outputdirectory);
                        break;
                    default:
                        outputdirectory = args[6].ToString();
                        Directory.CreateDirectory(outputdirectory);
                        break;
                }
            }
            else
            {
                switch (args[3].ToString())
                {
                    case "default":
                        css = "default";
                        break;
                    default:
                        css = args[3].ToString();
                        break;
                }

                switch (args[4].ToString())
                {
                    case "-o":
                        outputfile = args[5].ToString();
                        break;                    
                    default:
                        Console.WriteLine(HelpMessage);
                        return;
                }
            }

            string[] files = null;
            string file;
            Global.Plgns.FindPlugins();
            string inputextension;
            if (inputfile != "")
            {
                file = inputfile;
                inputextension = Global.fe.GetExtension(file);
                Global.th.SetLang(inputextension);
                Global.th.Parse(file, outputfile);
            }
            else
            {
                switch (find)
                {
                    case "all":
                        files = Directory.GetFiles(inputdirectory, "*", SearchOption.AllDirectories);
                        break;
                    case "current":
                        files = Directory.GetFiles(inputdirectory, "*", SearchOption.TopDirectoryOnly);
                        break;

                }
                for (int i = 0; i < files.Length; i++)
                {                    
                    inputextension = Global.fe.GetExtension(files[i]);
                    Global.th.SetLang(inputextension);
                    Global.th.Parse(files[i], outputdirectory + "\\" + i.ToString() + ".html");
                }
            }

             
        }
    }

}